package com.example.shcenterpid.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.shcenterpid.Bean.TicketResultBean;
import com.example.shcenterpid.Help.TimeUtil;
import com.example.shcenterpid.R;

import java.util.List;

import static com.example.shcenterpid.R.color.read;

public class TicketShowAdapter extends BaseAdapter {

    private Context mContext;
    private List<TicketResultBean.DataBean.TimeControlListBean> mList;

    public TicketShowAdapter(Context mContext, List<TicketResultBean.DataBean.TimeControlListBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        } else {
            return mList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService((Context.LAYOUT_INFLATER_SERVICE));
            convertView = layoutInflater.inflate(R.layout.view_time_show, null);
            viewHolder = new ViewHolder();
            viewHolder.mTime = convertView.findViewById(R.id.tv_time);
            viewHolder.mTicket = convertView.findViewById(R.id.tv_ticket_number);
            viewHolder.llView = convertView.findViewById(R.id.ll_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        try {
            viewHolder.mTime.setText((mList.get(position).getControlStartTime()).substring(0, 5) + "-" + (mList.get(position).getControlEndTime()).substring(0, 5));
            viewHolder.mTicket.setText(mList.get(position).getRemainSum() + " 张");
            if (mList.get(position).getRemainSum() <= 300) {
                viewHolder.mTicket.setTextColor(Color.RED);
            }
            //如果当前时间 大于 时段最晚时间，那么就说明时段已经过去了，变灰
            if (TimeUtil.pastTime(mList.get(position).getControlEndTime())) {
                viewHolder.llView.setBackgroundResource(R.drawable.shape_view_two);
                viewHolder.mTicket.setTextColor(Color.GRAY);
                viewHolder.mTime.setTextColor(Color.GRAY);
            } else if (mList.get(position).getRemainSum() <= 300) {
                viewHolder.llView.setBackgroundResource(R.drawable.shape_view_one);
                viewHolder.mTime.setTextColor(mContext.getResources().getColor(R.color.yellow3));
                viewHolder.mTicket.setTextColor(Color.RED);
            } else {
                viewHolder.llView.setBackgroundResource(R.drawable.shape_view_one);
                viewHolder.mTime.setTextColor(mContext.getResources().getColor(R.color.yellow3));
                viewHolder.mTicket.setTextColor(Color.GREEN);
            }
        } catch (Exception e) {

        }

        return convertView;
    }


    class ViewHolder {
        TextView mTime;
        TextView mTicket;
        LinearLayout llView;

    }
}
